package com.ndp.fb.scheduler.job.productfeed;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ndp.ec.core.CatalogService;
import com.ndp.ec.core.FeedService;
import com.ndp.ec.core.ProductSetService;
import com.ndp.ec.core.api.ProductFeedService;
import com.ndp.fb.constant.ConfigConst;
import com.ndp.fb.enums.ec.EcTemplateMaterielCreatedStatus;
import com.ndp.fb.rdb.api.EcTemplateMaterielRdbService;
import com.ndp.fb.rdb.model.EcTemplateMateriel;
import com.ndp.fb.scheduler.annotation.CronScheduled;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by Elvis on 2016/4/14.
 */
@Component
public class ProductFeedJob {

    private static Logger logger = LoggerFactory.getLogger(ProductFeedJob.class);

    @Reference
    private FeedService feedService;

    @Reference
    ProductFeedService productFeedService;
    @Reference
    private CatalogService catalogService;
    @Reference
    ProductSetService productSetService;

    @Reference
    private EcTemplateMaterielRdbService ecTemplateMaterielRdbService;

    @CronScheduled(cronkey = ConfigConst.SCHEDULE_PRODUCT_WEFEED_CRON, enablekey = ConfigConst.SCHEDULE_PRODUCT_WEFEED_ENABLE)
    public void weFeedSchedule() {
        logger.info("weFeedSchedule 在fb前两小时拉取 >>> ");
        feedService.weFeedSchedule();
    }

    @CronScheduled(cronkey = ConfigConst.SCHEDULE_PRODUCT_FEED_FETCH_CRON, enablekey = ConfigConst.SCHEDULE_PRODUCT_FEED_FETCH_ENABLE)
    public void fetchFeed() {
        logger.info("Product feed fetch 开始拉取 >>> ");
        productFeedService.updateFeedStatus();
    }
    
    @CronScheduled(cronkey = ConfigConst.SCHEDULE_PRODUCT_SET_CREATE_CRON, enablekey = ConfigConst.SCHEDULE_PRODUCT_SET_CREATE_ENABLE)
    public void createProductSet() {
        logger.info("Product feed fetch 开始拉取 >>> ");
        productSetService.excuteProductSet();
    }

    @CronScheduled(cronkey = ConfigConst.SCHEDULE_UPDATE_TEMPLATE_MATERIEL_CRON, enablekey = ConfigConst.SCHEDULE_UPDATE_TEMPLATE_MATERIEL_ENABLE)
    public void updateTemplateMateriel(){
        logger.info("updateTemplateMateriel>>> ");
        List<EcTemplateMateriel> allCreated = ecTemplateMaterielRdbService.findAllCreated();
        for (EcTemplateMateriel ecTemplateMateriel : allCreated) {
            if (EcTemplateMaterielCreatedStatus.CreateIng.getType() == ecTemplateMateriel.getCreatedStatus()){
                logger.info("ecTemplateMateriel>>>>" + ecTemplateMateriel.getId() + ";>>>name>>" + ecTemplateMateriel.getName()+";CreateIng");
            } else {
                logger.info("ecTemplateMateriel>>>>" + ecTemplateMateriel.getId() + ";>>>name>>" + ecTemplateMateriel.getName());
                productFeedService.excuteProductFeedService(ecTemplateMateriel);
            }
        }
    }

}
